package net.actions.sys;

import net.base.BaseAction;
import net.base.BaseDao;
import net.base.MD5;
import net.base.Tools;
import org.apache.log4j.Logger;

import java.util.Map;

import com.opensymphony.xwork2.Action;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-11-20
 * Time: 18:54:41
 * To change this template use File | Settings | File Templates.
 */
public class LoginAction extends BaseAction {
    private BaseDao dao = null;

    public BaseDao getDao() {
        return dao;
    }

    public void setDao(BaseDao dao) {
        this.dao = dao;
    }

    private Logger log = Logger.getLogger(this.getClass());

    public void doLogin() {
        try {
            Map map = this.parserRequest(this.getRequest());
            map.put("ACCOUNT", map.get("ACCOUNT"));
            Map userMap = dao.getMap("user.selectUserBY_ACC", map);
            MD5 md5 = new MD5();
            String pass = md5.getMD5ofStr(String.valueOf(map.get("USER_PWD")));
            System.out.println(userMap.get("YXBZ") + "     " + "1".equals(userMap.get("YXBZ")));
            if (userMap == null) {
                this.setResult("01", "该账号不存在！");
            } else if (!pass.equals(userMap.get("USER_PWD"))) {
                this.setResult("02", "用户密码不正确！");
            } else if (!"1".equals(userMap.get("YXBZ"))) {
                this.setResult("03", "该用户已被禁用！");
            } else {
                this.getSession().put("SYS_USER", userMap);
                this.setResult("00", Tools.toJsonByMap(userMap));
            }
        } catch (Exception e) {
            log.error(e.toString());
            this.setResult("99", "操作失败！\n" + e.toString());
        }
    }

    /**
     * 退出登录
     */
    public void loginOut() {
        this.getSession().clear();
        this.setResult("00", "操作成功");
    }

    /*
   取登录用户session
    */
    public void getUserSession() {
        String json = Tools.toJsonByMap((Map) this.getSession().get("SYS_USER"));
        this.setResult("00", json.substring(0, json.length() - 1));
    }
}
